package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 标签页
 * @author handinfo
 */
public class DWTabPage extends DWControl
{
	private CopyOnWriteArrayList<DWControl> m_controls = new CopyOnWriteArrayList<DWControl>();
	private DWScrollBar m_scrollbar = null;
	private boolean m_is_select = false;
	private DWFont m_font = DWFont.getDefaultFont();

	public DWTabPage()
	{

	}

	public void render(DWGraphics g)
	{
		if (m_is_select) {
			DWControl control;
			for (int i = 0; i < m_controls.size(); i++) {
				control = m_controls.get(i);
				if (control.m_is_show) {
					control.renderLimits(g, m_show_x, m_show_y, m_show_w, m_show_h, 0, 0);
				}
			}
		}
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (m_is_select) {
			renderLimitsRect(g, x, y, offsetx, offsety);
			DWControl control;
			for (int i = 0; i < m_controls.size(); i++) {
				control = m_controls.get(i);
				if (control.m_is_show) {
					control.renderLimits(g, x + m_show_x, y + m_show_y, m_show_w, m_show_h, offsetx, offsety);
				}
			}
		}
	}

	/**
	 * 绘制标签
	 * @param g
	 * @param name
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param select
	 */
	public void renderTag(DWGraphics g, Bitmap sImg, Bitmap nImg, String name, int x, int y, int w, int h, boolean select)
	{
		int tx = x + w / 2;
		int ty = y + h / 2;
		int nw = (int)m_font.stringWidth(name) / 2;
		if (select) {
			if (sImg != null && name != null) {
				g.drawBitmap(sImg, tx, y + h, Tools.HCENTER_BOTTOM);
				g.drawShadowString(m_font, name, DWControlsManager.COLOR_STROKE, Tools.WHITE, tx - nw, ty - (m_font.getHeight() / 2), Tools.LEFT_TOP);
			}
		}
		else {
			if (nImg != null && name != null) {
				g.drawBitmap(nImg, tx, y + h, Tools.HCENTER_BOTTOM);
				g.drawShadowString(m_font, name, DWControlsManager.COLOR_STROKE, Tools.WHITE, tx - nw, ty - (m_font.getHeight() / 2), Tools.LEFT_TOP);
			}
		}
	}
	
	public void setFont(DWFont font) {
		m_font = font;
	}
	/**
	 * 设置是否被选择
	 */
	public void setSelect(boolean select)
	{
		m_is_select = select;
	}

	/**
	 * 得到是否被选择
	 * @param select
	 */
	public boolean getSelect()
	{
		return m_is_select;
	}

	public void addControls(DWControl control)
	{
		if (control != null) {
			control.setRelyControl(this);
			m_controls.add(control);
			refreshControl();
		}
	}
	
	public void removeControl(DWControl control) {
		if (control != null) {
			m_controls.remove(control);
			refreshControl();
		}
	}
	
	public void removeAllControl() {
		m_controls.clear();
		if (m_scrollbar != null) {
			m_scrollbar.destroy();
			m_scrollbar = null;
		}
	}
	
	public boolean contains(DWControl control) {
		if (control != null) {
			return m_controls.contains(control);
		}
		return false;
	}

	/**
	 * 重写刷新父类层级控件,当TabPage内控件发生坐标,宽高改变时调用
	 */
	public void refreshControl()
	{
		DWControl control = null;
		int maxW = 0;
		int maxH = 0;
		for (int i = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			control.setDefaultNearAnchorUn();
		}
		for (int i = 0, w = 0, h = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			control.setPointUn(control.m_show_x - m_show_x, control.m_show_y - m_show_y);
			control.refreshControl();
			w = control.m_show_x + control.m_show_w;
			maxW = maxW < w ? w : maxW;
			h = control.m_show_y + control.m_show_h;
			maxH = maxH < h ? h : maxH;
		}
		if (maxH > m_show_h) {
			m_scrollbar = null;
			m_scrollbar = new DWScrollBar(m_show_h);
			m_scrollbar.setNearAnchor(this, Tools.LEFT_TOP, Tools.RIGHT_TOP, -m_scrollbar.getShowWidth(), 0);
			setActualWideHigh(maxW - m_scrollbar.getShowWidth(), maxH);
		}
		else {
			setActualWideHigh(maxW, maxH);
		}
	}

	public void logic()
	{
		DWControl control = null;
		for (int i = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			if (control != null) {
				control.logic();
			}
		}
	}
	
	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y);
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doDown(newPoint)) {
					return true;
				}
			}
			return true;
		}
		return false;
	}
	public boolean doLongPress(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y);
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doLongPress(newPoint)) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y)) {
			PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
			PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doScroll(newStart, newEnd, distanceX, distanceY)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
			PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doDrag(newStart, newEnd, velocityX, velocityY)) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
		PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
		for (int i = m_controls.size() - 1; i > -1; i--) {
			if (m_controls.get(i).doFling(newStart, newEnd, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).doClick(x - m_show_x, y - m_show_y)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean onTouch(MotionEvent event, PointF point) {
		if (checkPointOnRect(point.x, point.y)) {
			for (int i = m_controls.size() - 1; i > -1; i--) {
				if (m_controls.get(i).onTouch(event, new PointF(point.x - m_show_x, point.y - m_show_y))) {
					return true;
				}
			}
		}
		return false;
	}

}
